package com.android.ailaoshan.index.map;

import android.content.Context;
import android.location.LocationManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.index.fragment.MapFragment;
import com.android.ailaoshan.index.listener.ButtonClickListener;
import com.android.ailaoshan.index.listener.GPSStatusListener;
import com.android.ailaoshan.index.listener.MapScaleListener;

import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.qmuiteam.qmui.widget.QMUILoadingView;


public class MapConfig {

    private ArcGISMap mArcGISMap;
    /**
     * ImageLayer图层地址
     */
    private final String LAYER_URL = "http://202.203.134.147:6080/arcgis/rest/services/AilaoFeature/AilaoFeatureService/MapServer";

    private View mapFragment;
    private QMUILoadingView loadingView;
    private MapView mMapView;


    public MapConfig(View view,MapView mapView) {
        mArcGISMap = new ArcGISMap(Basemap.Type.IMAGERY, 25.063463,102.758171,16);
        ArcGISMapImageLayer imageLayer = new ArcGISMapImageLayer(LAYER_URL);
        mArcGISMap.getOperationalLayers().add(imageLayer);

        this.mapFragment = view;
        this.mMapView = mapView;
    }

    /**
     * 地图初始化
     */
    public void initMap(){
        // 显示正在加载
        loadingView = mapFragment.findViewById(R.id.qmui_loading);
        loadingView.setVisibility(View.VISIBLE);
        loadingView.setActivated(true);
        loadingView.start();

        if(mMapView != null){
            mMapView.setMap(mArcGISMap);
        }
    }

    /**
     * 地图加载监听
     */
    public void mapListener(Context context, MapFragment fragment){
        if(mArcGISMap != null){
            mArcGISMap.addDoneLoadingListener(
                () -> {
                    if(mArcGISMap.getLoadStatus().equals(LoadStatus.LOADED)){
                        // 停止显示正在加载
                        loadingView.setActivated(false);
                        loadingView.setVisibility(View.INVISIBLE);
                        loadingView.stop();

                        // 显示地图
                        mMapView.setVisibility(View.VISIBLE);

                        // 监听按钮
                        addViewListener(context,fragment);

                        // 显示设备位置
                        getDeviceLocation(context);
                    }
                }
            );
        }
    }


    /**
     * 控件监听
     */
    private void addViewListener(Context mContext,MapFragment fragment){
        if(mapFragment != null){

            if(mContext != null){
                // GPS状态监听
                TextView gpsStatus = mapFragment.findViewById(R.id.gps_status_txt);
                gpsStatus.setVisibility(View.VISIBLE);
                LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
                if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                    locationManager.addGpsStatusListener(new GPSStatusListener(gpsStatus, locationManager));
                }else{
                    Toast.makeText(mContext,"GPS不可用，请打开GPS开关",Toast.LENGTH_LONG).show();
                }
            }

            ButtonClickListener buttonClickListener = new ButtonClickListener(mMapView, mContext, fragment);

            // 定位按钮监听
            FloatingActionButton locationBtn = mapFragment.findViewById(R.id.getCurrentLocation);
            locationBtn.show();
            locationBtn.setOnClickListener(buttonClickListener);

            // 拍照按钮
            FloatingActionButton camera = mapFragment.findViewById(R.id.FloatingTakePhoto);
            camera.show();
            camera.setOnClickListener(buttonClickListener);

            // 录音按钮
            FloatingActionButton takeVoice = mapFragment.findViewById(R.id.takeVoice);
            takeVoice.show();
            takeVoice.setOnClickListener(buttonClickListener);

            // 地图放大
            ImageButton zoomIn = mapFragment.findViewById(R.id.mapView_zoomIn);
            zoomIn.setVisibility(View.VISIBLE);
            zoomIn.setOnClickListener(buttonClickListener);

            // 地图缩小
            ImageButton zoomOut = mapFragment.findViewById(R.id.mapView_zoomOut);
            zoomOut.setVisibility(View.VISIBLE);
            zoomOut.setOnClickListener(buttonClickListener);

            // 地图比例
            if(mMapView != null){
                mMapView.addMapScaleChangedListener(new MapScaleListener(zoomIn,zoomOut,mMapView));
            }
        }
    }

    /**
     * 显示设备位置
     */
    private void getDeviceLocation(Context mContext){
        if(this.mMapView != null){
            LocationDisplay locationDisplay = mMapView.getLocationDisplay();
            // LocationDisplay状态监听
            locationDisplay.addDataSourceStatusChangedListener(new LocationDisplay.DataSourceStatusChangedListener() {
                @Override
                public void onStatusChanged(LocationDisplay.DataSourceStatusChangedEvent dataSourceStatusChangedEvent) {

                    if (dataSourceStatusChangedEvent.isStarted() || dataSourceStatusChangedEvent.getError() == null) {
                        return;
                    }

                    String message = String.format("出现错误: %s",
                            dataSourceStatusChangedEvent.getSource().getLocationDataSource().getError().getMessage());
                    Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
                }
            });
            locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.COMPASS_NAVIGATION);
            locationDisplay.startAsync();
        }
    }
}
